using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Windows_Explorer.Forms
{
    public partial class frmRename : Form
    {
        public frmRename()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                string name = Program.fmain.lvListView.FocusedItem.SubItems[0].Text.ToString();
                DirectoryInfo di = new DirectoryInfo(Program.fmain.lvListView.FocusedItem.SubItems[4].Text.ToString());
                FileInfo fi = new FileInfo(Program.fmain.lvListView.FocusedItem.SubItems[4].Text.ToString());
                if (Program.fmain.isDir)//rename thu muc
                {
                    if (Microsoft.VisualBasic.FileIO.FileSystem.DirectoryExists(Program.fmain.currentPath) && txtTenCu.Text.Trim() != txtTenMoi.Text.Trim())
                    {
                        DirectoryInfo diNew = di.Parent;
                        foreach (DirectoryInfo d in diNew.GetDirectories())
                        {
                            if (d.Name.Trim().ToString() == txtTenMoi.Text.Trim().ToString())
                            {
                                MessageBox.Show("Tên thư mục này đã tồn tại!\nVui lòng chọn tên khác", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                                txtTenMoi.Text = name;
                                return;
                            }
                        }
                        Microsoft.VisualBasic.FileIO.FileSystem.RenameDirectory(Program.fmain.currentPath,
                            txtTenMoi.Text.Trim());
                        if (Program.fmain.nodeCurrent != null) Program.fmain.nodeCurrent.Text = txtTenMoi.Text;
                        else Program.fmain.lviCurrent.Text = txtTenMoi.Text;
                        this.Close();
                    }
                }
                else //rename file
                {
                    if (Microsoft.VisualBasic.FileIO.FileSystem.FileExists(Program.fmain.currentPath) && txtTenCu.Text.Trim() != txtTenMoi.Text.Trim())
                    {
                        DirectoryInfo diNew = fi.Directory;
                        foreach (FileInfo f in diNew.GetFiles())
                        {
                            if (f.Name.Trim().ToString() == txtTenMoi.Text.Trim().ToString())
                            {
                                MessageBox.Show("Tên file này đã tồn tại!\nVui lòng chọn tên khác", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                                txtTenMoi.Text = name;
                                return;
                            }
                        }
                        Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(Program.fmain.currentPath,
                            txtTenMoi.Text.Trim());
                        Program.fmain.lviCurrent.Text = txtTenMoi.Text;
                        this.Close();
                    }
                }
            }
            catch { }
        }

        private void frmRename_Load(object sender, EventArgs e)
        {
            txtTenCu.Text = Program.fmain.currentName;
            txtTenMoi.Focus();
        }

        
    }
}